﻿using System;
using System.Windows.Markup;
using System.Xml;
using System.IO;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Xml.Linq;
using Groupbox;
using System.Windows.Browser;


namespace OntologyWPFSilverlight
{
    
    public partial class MainPage : UserControl
    {
       
        XDocument doc = new XDocument();
        
        public MainPage()
        {
            InitializeComponent();


            //WebClient wc = new WebClient();
            //wc.OpenReadCompleted += wc_OpenReadCompleted;
            //wc.OpenReadAsync(new Uri("http://localhost:4517/ClientBin/saveFileDialog.xml", UriKind.Absolute));
       
        }


        private void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
        {
            if (e.Error != null)
            {
                //OutputTextBlock.Text = e.Error.Message;
                return;
            }
            using (Stream s = e.Result)
            {
                XDocument doc = XDocument.Load(s);

                IEnumerable<XElement> elements = doc.Root.Descendants("Widget");

                Canvas myCanvas = new Canvas();

                

                Border border = new Border();
                border.BorderBrush = new SolidColorBrush(Colors.Black);
                border.BorderThickness = new Thickness(5);
                border.Child = myCanvas;

                Slider slider = new Slider();
                byte r, g, b;

                foreach (XElement el in elements)
                {
                    string type = el.Attribute("Type").Value.Split('.')[el.Attribute("Type").Value.Split('.').Length - 1];

                    //OutputTextBlock.Text += type;
                    switch (type)
                    {
                        #region Form
                        case "Form":

                            border.Width = Int32.Parse(el.Element("Size").Element("Width").Attribute("Value").Value);
                            border.Height = Int32.Parse(el.Element("Size").Element("Height").Attribute("Value").Value);
                            
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[0], out r);
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[1], out g);
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[2], out b);
                            myCanvas.Background = new SolidColorBrush(Color.FromArgb(255, r, g, b));
                            //string a = el.Element("Appearance").Element("BackColor").Attribute("Value").Value;
                            break;
                        #endregion

                        #region ListBox
                        case "ListBox":

                            Widget.Listbox listbox1 = new Widget.Listbox();
                            listbox1.Width = Int32.Parse(el.Element("Size").Element("Width").Attribute("Value").Value);
                            listbox1.Height = Int32.Parse(el.Element("Size").Element("Height").Attribute("Value").Value);
                            Canvas.SetLeft(listbox1, Int32.Parse(el.Element("Location").Element("X").Attribute("Value").Value));
                            Canvas.SetTop(listbox1, Int32.Parse(el.Element("Location").Element("Y").Attribute("Value").Value));
                           
                            
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[0], out r);
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[1], out g);
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[2], out b);
                            listbox1.Background=new SolidColorBrush(Color.FromArgb(255,r,g,b));

                            if (!el.Element("Children").Attribute("count").Value.Equals("0"))
                            {
                                IEnumerable<XElement> nodes = el.Element("OntologyNodes").Descendants("Node");
                                listbox1.AddItem(nodes);   
                            }

                            listbox1.AddLabel(el.Element("OntologyNode").Attribute("Alias").Value);

                            myCanvas.Children.Add(listbox1);
                            break;
                        #endregion

                        #region ComboBox
                        case "ComboBox":
                            
                            Widget.ComboBox comboBox = new Widget.ComboBox();
                            
                            comboBox.Width = Int32.Parse(el.Element("Size").Element("Width").Attribute("Value").Value);
                            comboBox.Height = Int32.Parse(el.Element("Size").Element("Height").Attribute("Value").Value);
                            Canvas.SetLeft(comboBox, Int32.Parse(el.Element("Location").Element("X").Attribute("Value").Value));
                            Canvas.SetTop(comboBox, Int32.Parse(el.Element("Location").Element("Y").Attribute("Value").Value));
                            
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[0], out r);
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[1], out g);
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[2], out b);
                            comboBox.Background = new SolidColorBrush(Color.FromArgb(255, r, g, b));

                            if (!el.Element("Children").Attribute("count").Value.Equals("0"))
                            {
                                IEnumerable<XElement> nodes = el.Element("OntologyNodes").Descendants("Node");
                                comboBox.AddItem(nodes);   
                            }

                            comboBox.AddLabel(el.Element("OntologyNode").Attribute("Alias").Value);

                            myCanvas.Children.Add(comboBox);
                            
                            break;
                        #endregion

                        #region GroupCheckBox
                        case "GroupCheckBox":

                            Widget.GroupCheckBox groupCheckBox = new Widget.GroupCheckBox();

                            groupCheckBox.Width = Int32.Parse(el.Element("Size").Element("Width").Attribute("Value").Value);
                            groupCheckBox.Height = Int32.Parse(el.Element("Size").Element("Height").Attribute("Value").Value);

                            Canvas.SetLeft(groupCheckBox, Int32.Parse(el.Element("Location").Element("X").Attribute("Value").Value));
                            Canvas.SetTop(groupCheckBox, Int32.Parse(el.Element("Location").Element("Y").Attribute("Value").Value));
                            
                           
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[0], out r);
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[1], out g);
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[2], out b);
                            groupCheckBox.Background = new SolidColorBrush(Color.FromArgb(255, r, g, b));

                            if (!el.Element("Children").Attribute("count").Value.Equals("0"))
                            {
                                IEnumerable<XElement> nodes = el.Element("OntologyNodes").Descendants("Node");
                                groupCheckBox.AddItem(nodes);
                            }

                            groupCheckBox.AddLabel(el.Element("OntologyNode").Attribute("Alias").Value);

                            myCanvas.Children.Add(groupCheckBox);

                            break;
                        #endregion

                        #region RadioGroupBox
                       
                        case "RadioGroupBox":
                            Widget.RadioGroupBox radioGroupBox = new Widget.RadioGroupBox();

                            radioGroupBox.Width = Int32.Parse(el.Element("Size").Element("Width").Attribute("Value").Value);
                            radioGroupBox.Height = Int32.Parse(el.Element("Size").Element("Height").Attribute("Value").Value);

                            Canvas.SetLeft(radioGroupBox, Int32.Parse(el.Element("Location").Element("X").Attribute("Value").Value));
                            Canvas.SetTop(radioGroupBox, Int32.Parse(el.Element("Location").Element("Y").Attribute("Value").Value));
                            
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[0], out r);
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[1], out g);
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[2], out b);
                            radioGroupBox.Background = new SolidColorBrush(Color.FromArgb(255, r, g, b));

                            if (!el.Element("Children").Attribute("count").Value.Equals("0"))
                            {
                                IEnumerable<XElement> nodes = el.Element("OntologyNodes").Descendants("Node");
                                radioGroupBox.AddItem(nodes);
                            }

                            radioGroupBox.AddLabel(el.Element("OntologyNode").Attribute("Alias").Value);

                            myCanvas.Children.Add(radioGroupBox);

                            break;
                        
                        #endregion

                        #region Slider
                        
                        case "Slider" :

                            Widget.Slider mySlider = new Widget.Slider();
                            
                            mySlider.Width = Int32.Parse(el.Element("Size").Element("Width").Attribute("Value").Value);
                            mySlider.Height = Int32.Parse(el.Element("Size").Element("Height").Attribute("Value").Value);

                            Canvas.SetLeft(mySlider, Int32.Parse(el.Element("Location").Element("X").Attribute("Value").Value));
                            Canvas.SetTop(mySlider, Int32.Parse(el.Element("Location").Element("Y").Attribute("Value").Value));

                           
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[0], out r);
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[1], out g);
                            byte.TryParse(el.Element("Appearance").Element("BackColor").Attribute("Value").Value.Split(',')[2], out b);
                            mySlider.Background = new SolidColorBrush(Color.FromArgb(255, r, g, b));

                            if (!el.Element("Children").Attribute("count").Value.Equals("0"))
                            {
                                IEnumerable<XElement> nodes = el.Element("OntologyNodes").Descendants("Node");
                                mySlider.AddItem(nodes);
                            }


                            break;
                        
                        #endregion






                    }


                }

                LayoutRoot.Children.Add(border);
            }
        }        
       
 }
}
